package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.application.AppManager;
import com.gluonhq.charm.glisten.control.AppBar;
import javafx.scene.Node;
import javafx.scene.control.MenuItem;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class AppBarActions {
    static{
        PropertyFactory.regist(AppBar.class, "autoHideProgressBarProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.autoHideProgressBarProperty();
        });
        PropertyFactory.regist(AppBar.class, "progressBarVisibleProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.progressBarVisibleProperty();
        });
        PropertyFactory.regist(AppBar.class, "titleProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.titleProperty();
        });
        PropertyFactory.regist(AppBar.class, "progressProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.progressProperty();
        });
        PropertyFactory.regist(AppBar.class, "titleTextProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.titleTextProperty();
        });
        PropertyFactory.regist(AppBar.class, "navIconProperty", o -> {
            AppBar obj = (AppBar) o;
            return obj.navIconProperty();
        });
    }

    public static void init(AppBar appBar, Thing thing, ActionContext actionContext){
        ControlActions.init(appBar, thing, actionContext);

        String titleText = JavaFXUtils.getString(thing, "titleText", actionContext);
        if(titleText != null){
            appBar.setTitleText(titleText);
        }

        if(thing.valueExists("title")){
            Node title = JavaFXUtils.getObject(thing, "title", actionContext);
            if(title != null) {
                appBar.setTitle(title);
            }
        }

        if(thing.valueExists("autoHideProgressBar")){
            appBar.setAutoHideProgressBar(thing.getBoolean("autoHideProgressBar"));
        }

        if(thing.valueExists("navIcon")){
            Node navIcon = JavaFXUtils.getObject(thing, "navIcon", actionContext);
            if(navIcon != null) {
                appBar.setNavIcon(navIcon);
            }
        }

        if(thing.valueExists("progressBarVisible")){
            appBar.setProgressBarVisible(thing.getBoolean("progressBarVisible"));
        }

        if(thing.valueExists("spacing")){
            appBar.setSpacing(thing.getDouble("spacing"));
        }
    }

    public static void doInit(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        System.out.println("Update app bar, path=" + self.getMetadata().getPath());
        AppBar appBar = actionContext.getObject("appBar");

        if(self.getBoolean("clear")) {
            appBar.getActionItems().clear();
            appBar.getMenuItems().clear();
        }

        init(appBar, self, actionContext);

        actionContext.peek().put("parent", appBar);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }
    }

    //xworker.gluon.glisten.control.AppBar/@actions/@create
    public static void create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        System.out.println("Create app bar, path=" + self.getMetadata().getPath());
        AppBar appBar = AppManager.getInstance().getAppBar();

        if(self.getBoolean("clear")) {
            appBar.getActionItems().clear();
            appBar.getMenuItems().clear();
        }

        init(appBar, self, actionContext);

        actionContext.peek().put("parent", appBar);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }
    }

    //xworker.gluon.glisten.control.AppBar/@NavIcon/@actions/@create
    public static void createNavIcon(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        System.out.println("Create nav icon, path=" + self.getMetadata().getPath());
        AppBar appBar = actionContext.getObject("parent");

        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof  Node){
                appBar.setNavIcon((Node) obj);
                return;
            }
        }
    }

    //xworker.gluon.glisten.control.AppBar/@MenuItems/@actions/@create
    public static void createMenuItems(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        System.out.println("Create menu items, path=" + self.getMetadata().getPath());
        AppBar appBar = actionContext.getObject("parent");

        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof MenuItem){
                appBar.getMenuItems().add((MenuItem) obj);
                return;
            }
        }
    }

    //xworker.gluon.glisten.control.AppBar/@ActionItems/@actions/@create
    public static void createActionItems(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        System.out.println("Create action items, path=" + self.getMetadata().getPath());
        AppBar appBar = actionContext.getObject("parent");

        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof  Node){
                appBar.getActionItems().add((Node) obj);
                return;
            }
        }
    }
}
